package com.myBlast.client;

import com.extjs.gxt.ui.client.widget.Dialog;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.NamedFrame;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class WidgetUtilities {

	public static Dialog makeDialog(String title, String question) {
		Dialog dialog = new Dialog();
		dialog.setHeading(title);
		dialog.addText(question);
		dialog.setBodyStyle("fontWeight:bold;padding:13px;");
		dialog.setSize(300, 100);
		dialog.setHideOnButtonClick(true);
		dialog.setButtons(Dialog.YESNOCANCEL);
		return dialog;
	}
	
	public static Tracker getTracker()
	{
		WidgetUtilities utilities = new WidgetUtilities();
		return (utilities.new Tracker());
	}
	
	public class Tracker 
	{
		public boolean status = false;
		public boolean getStatus(){return status;}
		public void setStatus(boolean stat){status = stat;}
	}
	
	/**
	 * Injects a hidden iframe. Mainly used for providing 
	 * download file facility. It removes the previouslt created ifram
	 * by the same id. Id of the iframe is downloadiframe
	 * @param result Name of the file to download
	 */
	public static void addDownloadiframe(String result, String projectName)
	{
		boolean frameExists = (RootPanel.get("downloadiframe") != null);
		if(frameExists) {
			Widget widgetFrame = (Widget)RootPanel.get("downloadiframe");
			widgetFrame.removeFromParent();
		}

		NamedFrame frame = new NamedFrame("downloadiframe");
		frame.setUrl(GWT.getModuleBaseURL() + "blastService" + "?file=" + result + "&folder=" + projectName);
		frame.setVisible(false);
		RootPanel.get().add(frame);
	}

	public static void displayFetching()
	{
		Element element = RootPanel.get("Fetching").getElement();
		element.setAttribute("style", "display: visible");
	}

	public static void hideFetching()
	{
		Element element = RootPanel.get("loading").getElement();
		element.setAttribute("style", "display: none");
	}

	public static void displayLoading()
	{
		Element element = RootPanel.get("loading").getElement();
		element.setAttribute("style", "display: visible");
	}

	public static void hideLoading()
	{
		Element element = RootPanel.get("loading").getElement();
		element.setAttribute("style", "display: none");
	}

	public static boolean isLoadPresent()
	{
		Element element = RootPanel.get("loading").getElement();
		String style = element.getPropertyString("style").split(":")[1].trim();
		if (style.equalsIgnoreCase("visible")) return true;
		return false;
	}
}
